package com.guanyong.admin.controllers.api;

import com.guanyong.model.MessageModel;
import com.guanyong.services.MessageService;
import com.guanyong.utils.Comm.ResultCode;
import com.guanyong.utils.Comm.ResultObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * Created by mxk on 2017/6/6.
 */
@RestController
@RequestMapping("/api/message/")
public class MeeageApi {
    @Autowired
    MessageService messageService;

    @RequestMapping(value = "messagelist", method = RequestMethod.GET)
    ResultObject<HashMap<String,Object>> getMessageList(HttpServletRequest request,
                                                         @RequestParam String title,
                                                         @RequestParam Integer show,
                                                         @RequestParam Integer ispublic,
                                                         @RequestParam int pno,
                                                         @RequestParam int pagesize,
                                                         @RequestParam int allcount) {
        ResultObject<HashMap<String,Object>> result = new ResultObject<HashMap<String,Object>>();
        MessageModel messageModel =new MessageModel();
        HashMap<String,Object> hashMap=messageService.GetMessageList(messageModel,pno,
                pagesize,allcount);
        result.setResult(hashMap);
        return result;
    }//updatestate

    @RequestMapping(value = "updatestate", method = RequestMethod.POST)
    ResultObject<Integer> UpdateState(HttpServletRequest request,
                                      @RequestParam Integer fid,
                                      @RequestParam Integer state){
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        MessageModel messageModel=new MessageModel();
        messageModel.setfId(fid);
        messageModel.setfShow(state);

        Integer res=messageService.UpdateMessgae(messageModel);
        if (res >-1){
            resultObject.setCode(ResultCode.OperateOk);
        }
        else{
            resultObject.setCode(ResultCode.OperateFail);
        }
        return  resultObject;
    }
    @RequestMapping(value = "addmessage", method = RequestMethod.POST)
    ResultObject<Integer> UpdateState(HttpServletRequest request,
                                      @RequestParam String  message_title,
                                      @RequestParam String message_touserid,
                                      @RequestParam String message_body){
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        MessageModel messageModel=new MessageModel();
        messageModel.setfTitle(message_title);
        messageModel.setfTouserid(message_touserid);
        messageModel.setfBody(message_body);

        Integer res=messageService.AddMessage(messageModel);
        if (res >-1){
            resultObject.setCode(ResultCode.OperateOk);
        }
        else{
            resultObject.setCode(ResultCode.OperateFail);
        }
        return  resultObject;
    }


    @RequestMapping(value = "updatemessage", method = RequestMethod.POST)
    ResultObject<Integer> UpdateState(HttpServletRequest request,
                                      @RequestParam Integer message_id,
                                      @RequestParam String  message_title,
                                      @RequestParam String message_touserid,
                                      @RequestParam String message_body){
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        MessageModel messageModel=new MessageModel();
        messageModel.setfId(message_id);
        messageModel.setfTitle(message_title);
        messageModel.setfTouserid(message_touserid);
        messageModel.setfBody(message_body);

        Integer res=messageService.UpdateMessgae(messageModel);
        if (res >-1){
            resultObject.setCode(ResultCode.OperateOk);
        }
        else{
            resultObject.setCode(ResultCode.OperateFail);
        }
        return  resultObject;
    }
}
